package edu.xidian.onlinedocument.service;

import edu.xidian.onlinedocument.entity.DocumentData;
import edu.xidian.onlinedocument.entity.UserData;
import edu.xidian.onlinedocument.handler.excpetions.custom.CustomException;
import edu.xidian.onlinedocument.views.ResultCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 提取、二次封装类
 * 含义全写在方法名里了
 */
@Component
@NoArgsConstructor
public class CustomDraw {
    /**
     * UserData处理
     */
    public UserData takeUserNumAndName(UserData data) {
        UserData userData = new UserData();
        userData.setUserNum(data.getUserNum());
        userData.setUserName(data.getUserName());
        return userData;
    }

    public UserData takeUserNumAndNameAndDocuments(UserData data) {
        UserData userData = new UserData();
        userData.setUserNum(data.getUserNum());
        userData.setUserName(data.getUserName());
        userData.setCreateDocumentData(data.getCreateDocumentData());
        return userData;
    }

    public UserData takeUserNumAndNameAndEditors(UserData data) {
        UserData userData = new UserData();
        userData.setUserNum(data.getUserNum());
        userData.setUserName(data.getUserName());
        userData.setEditDocumentData(data.getEditDocumentData());
        return userData;
    }

    public UserData takeUserNumAndNameAndDocumentsAndEditors(UserData data) {
        UserData userData = new UserData();
        userData.setUserNum(data.getUserNum());
        userData.setUserName(data.getUserName());
        userData.setCreateDocumentData(data.getCreateDocumentData());
        userData.setEditDocumentData(data.getEditDocumentData());
        return userData;
    }

    public List<UserData> takeUserNumAndName(List<UserData> datas) {
        List<UserData> userDatas = new ArrayList<>();
        for (UserData data:
                datas) {
            UserData newData = new UserData();
            newData.setUserNum(data.getUserNum());
            newData.setUserName(data.getUserName());
            userDatas.add(newData);
        }
        return userDatas;
    }

    public List<UserData> takeUserNumAndNameAndDocuments(List<UserData> datas) {
        List<UserData> userDatas = new ArrayList<>();
        for (UserData data:
                datas) {
            UserData newData = new UserData();
            newData.setUserNum(data.getUserNum());
            newData.setUserName(data.getUserName());
            newData.setCreateDocumentData(data.getCreateDocumentData());
            userDatas.add(newData);
        }
        return userDatas;
    }

    public List<UserData> takeUserNumAndNameAndEditors(List<UserData> datas) {
        List<UserData> userDatas = new ArrayList<>();
        for (UserData data:
                datas) {
            UserData newData = new UserData();
            newData.setUserNum(data.getUserNum());
            newData.setUserName(data.getUserName());
            newData.setEditDocumentData(data.getEditDocumentData());
            userDatas.add(newData);
        }
        return userDatas;
    }

    public List<UserData> takeUserNumAndNameAndDocumentsAndEditors(List<UserData> datas) {
        List<UserData> userDatas = new ArrayList<>();
        for (UserData data:
                datas) {
            UserData newData = new UserData();
            newData.setUserNum(data.getUserNum());
            newData.setUserName(data.getUserName());
            newData.setCreateDocumentData(data.getCreateDocumentData());
            newData.setEditDocumentData(data.getEditDocumentData());
            userDatas.add(newData);
        }
        return userDatas;
    }

    /**
     * DocumentData处理
     */
    public DocumentData takeDocumentInfo(DocumentData documentData) {
        DocumentData newData = new DocumentData();
        newData.setDocId(documentData.getDocId());
        newData.setDocName(documentData.getDocName());
        newData.setDocVersion(documentData.getDocVersion());
        newData.setDocIsDelete(documentData.getDocIsDelete());
        newData.setDocCreateTime(documentData.getDocCreateTime());
        newData.setDocUpdateTime(documentData.getDocUpdateTime());
        return newData;
    }

    public DocumentData takeDocumentInfoAndUserInfo(DocumentData documentData) {
        DocumentData newData = this.takeDocumentInfo(documentData);
        newData.setCreatorData(this.takeUserNumAndName(documentData.getCreatorData()));
        return newData;
    }

    public DocumentData takeDocumentInfoAndEditors(DocumentData documentData) {
        DocumentData newData = this.takeDocumentInfo(documentData);
        newData.setDocumentEditor(documentData.getDocumentEditor());
        return newData;
    }

    public DocumentData takeDocumentInfoAndUserInfoAndEditors(DocumentData documentData) {
        DocumentData newData = this.takeDocumentInfo(documentData);
        newData.setDocumentEditor(documentData.getDocumentEditor());
        newData.setCreatorData(this.takeUserNumAndName(documentData.getCreatorData()));
        return newData;
    }

    public DocumentData takeDocumentInfoAndOperationData(DocumentData documentData) {
        DocumentData newData = new DocumentData();
        newData.setDocId(documentData.getDocId());
        newData.setDocName(documentData.getDocName());
        newData.setDocVersion(documentData.getDocVersion());
        newData.setOperationData(documentData.getOperationData());
        return newData;
    }

    public List<DocumentData> takeDocumentInfo(List<DocumentData> documentData) {
        List<DocumentData> newData = new ArrayList<>();
        for (DocumentData data:
             documentData) {
            newData.add(this.takeDocumentInfo(data));
        }
        return newData;
    }

    public List<DocumentData> takeDocumentInfoAndUserInfo(List<DocumentData> documentData) {
        List<DocumentData> newData = new ArrayList<>();
        for (DocumentData data:
                documentData) {
            newData.add(this.takeDocumentInfoAndUserInfo(data));
        }
        return newData;
    }

    public List<DocumentData> takeDocumentInfoAndEditors(List<DocumentData> documentData) {
        List<DocumentData> newData = new ArrayList<>();
        for (DocumentData data:
                documentData) {
            newData.add(this.takeDocumentInfoAndEditors(data));
        }
        return newData;
    }

    public List<DocumentData> takeDocumentInfoAndUserInfoAndEditors(List<DocumentData> documentData) {
        List<DocumentData> newData = new ArrayList<>();
        for (DocumentData data:
                documentData) {
            newData.add(this.takeDocumentInfoAndUserInfoAndEditors(data));
        }
        return newData;
    }

    public List<DocumentData> takeDocumentInfoAndOperationData(List<DocumentData> documentData) {
        List<DocumentData> newData = new ArrayList<>();
        for (DocumentData data:
                documentData) {
            newData.add(this.takeDocumentInfoAndOperationData(data));
        }
        return newData;
    }

    public void checkDocId(Integer docId) {
        if(docId.equals(null)) {
            throw new CustomException(ResultCode.DOCUMENT_ID_IS_NULL);
        }
    }

    public void checkDocName(String docName) {
        if(StringUtils.isBlank(docName)) {
            throw new CustomException(ResultCode.DOCUMENT_NAME_IS_NULL);
        }
    }

    public void checkUserId(Integer userId) {
        if(userId.equals(null)) {
            throw new CustomException(ResultCode.USER_ID_IS_NULL);
        }
    }

    public void checkUserNum(String userNum) {
        if(StringUtils.isBlank(userNum)) {
            throw new CustomException(ResultCode.USER_ACCOUNT_IS_NULL);
        }
    }

    public void checkUserToken(String userToken) {
        if(StringUtils.isBlank(userToken)) {
            throw new CustomException(ResultCode.TOKEN_IS_NULL);
        }
    }

    public void checkDate(Date time) {
        if(time == null) {
            throw new CustomException(ResultCode.DATE_IS_NULL);
        }
    }

    public void checkVersion(Integer opVersion) {
        if(opVersion == null) {
            throw new CustomException(ResultCode.OPERATION_VERSION_IS_NULL);
        }
    }
}
